<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * 模块:后台会员模块
 * 开发:胡耆英
 * 创建日期: 2011-05-30
 * 
 */
class Member extends B_Controller
{
	public function __construct()
	{
		parent::__construct();
		parent::check_login();
	}
	/*
	*会员列表
	*/
	public function index()
	{
		
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post')
		{
			$account= trim($this->input->post('account'));
			if(!empty($account))
			{
				$where['account'] = $account; 
			}
			$mobile = trim($this->input->post('mobile'));
			if(!empty($mobile))
			{
				$where['mobile'] = $mobile; 
			}
			$data['member']['data'][] = M('member')->where($where)->find();
		}
		else
		{
			$num = M('member')->field('count(*) as num')->find();
			$data['member'] = M('member')->order('id desc')->findpage(15,$num['num']);
		}
		$data['title'] = '会员查询管理';
		$data['keyword'] = '会员列表';
		$data['account']=!empty($account)?$account:'';
		$data['mobile']=!empty($mobile)?$mobile:'';
		$this->load->view('member',$data);
	}
	
	/*
	*添加会员
	*/	
	public function add()
	{
		if(strtolower($_SERVER['REQUEST_METHOD'])=='post')
		{
			$data['account'] = $this->input->post('account');
			$data['type'] = $this->input->post('member_type');
			$data['name'] = $this->input->post('name');
			$data['mobile'] = $this->input->post('mobile');
			$data['qq'] = $this->input->post('qq');
			$data['email'] = $this->input->post('email');
			$data['sex'] = $this->input->post('sex');
			$data['habitude'] = $this->input->post('habitude');
			$data['lyear'] = $this->input->post('lyear');
			$data['lbirthday'] = $this->input->post('lbirthday');
			$data['syear'] = $this->input->post('syear');
			$data['sbirthday'] = $this->input->post('sbirthday');
			$data['address'] = $this->input->post('address');
			$data['others'] = $this->input->post('others');
			$data['operator'] = $this->session->userdata('username');
			$data['datetime'] = time();
			$this->load->model('member_model');
			$result = $this->member_model->add_member($data);
			if($result)
			{
				 show_message('添加成功!', site_url('member/index'),1);
			}
			else
			{
				show_message('添加失败!', site_url('member/add'),1);
			}
		}
		else
		{
			$data['title'] = '添加会员';
			$data['keyword'] = '添加会员';
			$this->load->view('member_add',$data);
		}
		return false;
	}
	/*
	*通过ajax方式来检查用户是否存在
	*/
	public function check_account()
	{
		$account = $this->input->post('account');
		$this->load->model('member_model');
		$result = $this->member_model->check_account($account);
		if(!empty($result['account']))
		{
			echo 'exist';
		}
		else
		{
			echo 'inexistence';
		}
	}
    
    /*
     * 会员详细
     */
    public function detail()
    {
        $id = $this->uri->segment(3);
        //这里不考虑id存不存在的问题了
        $data['member'] = M('member')->where(array('id'=>$id))->find();
        if(empty($data['member']))
        {
            show_message('用户不存在!',site_url('member/index'),1);
            return false;
        }
        else
        {
            //订单
            $orders = M('orders')->field('count(*) as num')->where(array('mid'=>$id))->find();
            //$data['orders'] = M('orders')->where(array('mid'=>$id))->findpage(20,$orders['num']);
            $limit = 15;
            $page = $this->input->get('p',TRUE);
            $page = (!empty($page) && is_numeric($page)&&$page>0) ? intval($page) : 1;
            $page = $page>$orders['num']?$orders['num']:$page;
            $page = $page==0?1:$page;
            $offset = ($page - 1) * $limit;
            $this->load->model('member_model');
            $data['orders'] = $this->member_model->check_order(array('orders.mid'=>$id),$limit,$offset,$orders['num'],$page);
            $data['title'] = '会员详情';
            $data['keyword'] = '会员详情';
            $this->load->view('member_detail',$data);
        }
    }
    
    /*
     * 订单详情
     * 这个可以考虑使用ajax来实现,体验会好点.
     */
    public function order_detail()
    {
		$oid = $this->uri->segment(3);
		$data['orders'] = M('orderbag')->where(array('oid'=>$oid))->findall();
		$this->load->view('order_input',$data);
    }
    
    /*
     * 今日生日的会员
     */
    public function birthday()
    {
		//输入今天生日的格式
		//阴历生日格式 05-11
		$lunar = $this->_get_lunar();
		//阳历生日格式 06-12等格式
		$solar = date('m-d',time());
		$where = "(`habitude` = 1 AND `sbirthday`='{$solar}') or (`habitude` = 0 AND `lbirthday`='{$lunar}')";
		$num = M('member')->field('count(*) as num')->where($where)->find();
		$data['member'] = M('member')->where($where)->findpage(15,$num['num']);
		$data['title'] = '今日生日的会员';
		$data['keyword'] = '生日会员';
		$this->load->view('member_birthday',$data);
    }
	
	//获取今天的阴历日期 没有考虑闰月,呵呵
	function _get_lunar(){
		$this->load->library('lunar');
		$this->lunar->calendar(date("Y"),date("m"),date("d"));   
		$month = $this->lunar->month;
		if($month<10)
		{
			$smonth = '0'.$month;
		}
		else
		{
			$smonth = $month;
		}
		$day = $this->lunar->day; 
		if($day<10)
		{
			$sday = '0'.$day;
		}
		else
		{
			$sday = $day;
		}
		return $smonth.'-'.$sday;
	}
}